package graph;


import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import menuListener.BackGroundListener;
import menuListener.GameMenuItemListener;
import menuListener.HelpMenuItemListener;
import menuListener.LevelMenuItemListener;
import model.GameModel;
import tool.ReadConfig;
import tool.ReadResources;

public class GameGraph extends JFrame implements ComponentListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PaintPanel paint;
	private GameModel gameModel;//游戏模型
	private ReadResources readResources;
	private ReadConfig readConfig;//配置读取器
	public GameGraph() {
		readConfig=new ReadConfig();
		readResources=new ReadResources(readConfig);
		gameModel=new GameModel(readConfig.getDefaultLevel());
		//gameModel=new GameModel(-1);
		setSize(900, 720);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口就退出程序
		paint=new PaintPanel(gameModel,readResources);
		add(paint);//设置窗口面板为绘图对象
		setTitle("纸牌游戏");
		addComponentListener(this);

		JMenuBar menuBar=new JMenuBar();
		setJMenuBar(menuBar);
		menuBar.setPreferredSize(new Dimension(200, 30));
		//设置游戏菜单项
		JMenu game=new JMenu("游戏");
		JMenuItem refresh=new JMenuItem("洗牌");
		game.add(refresh);
		refresh.addActionListener(new GameMenuItemListener(gameModel,paint));
		//设置难度菜单项
		JMenu level=new JMenu("难度");
		JRadioButtonMenuItem hard=new JRadioButtonMenuItem("难");
		JRadioButtonMenuItem medium=new JRadioButtonMenuItem("中");
		JRadioButtonMenuItem easy=new JRadioButtonMenuItem("易");
		ButtonGroup buttonGroup=new ButtonGroup();
		buttonGroup.add(easy);
		buttonGroup.add(medium);
		buttonGroup.add(hard);
		hard.addActionListener(new LevelMenuItemListener(paint,level,gameModel,readConfig));	
		medium.addActionListener(new LevelMenuItemListener(paint,level,gameModel,readConfig));
		easy.addActionListener(new LevelMenuItemListener(paint,level,gameModel,readConfig));
		level.add(easy);
		level.add(medium);
		level.add(hard);
		if(gameModel.getLevel()>=0)
			level.getItem(gameModel.getLevel()).setSelected(true);
		//设置背景菜单项
		JMenu background=new JMenu("背景");
		JMenu pokerBack=new JMenu("牌背景");
		for(String name:readResources.getPokerBackChoice()){
			JMenuItem menuItem=new JMenuItem(name);
			pokerBack.add(menuItem);
			menuItem.addActionListener(new BackGroundListener(readResources, paint, 0,readConfig));
		}
		JMenu tableBack=new JMenu("牌桌背景");
		for(String name:readResources.getTableBackChoice()){
			JMenuItem menuItem=new JMenuItem(name);
			tableBack.add(menuItem);
			menuItem.addActionListener(new BackGroundListener(readResources, paint, 1,readConfig));
		}
		background.add(pokerBack);
		background.add(tableBack);
		//设置帮助菜单项
		JMenu help=new JMenu("帮助");
		JMenuItem levelHelp=new JMenuItem("关于难度");
		JMenuItem backHelp=new JMenuItem("关于背景");
		help.add(levelHelp);
		help.add(backHelp);
		levelHelp.addActionListener(new HelpMenuItemListener(this));
		backHelp.addActionListener(new HelpMenuItemListener(this));
		//将所有菜单项加到菜单条里面
		menuBar.add(game);
		menuBar.add(level);
		menuBar.add(background);
		menuBar.add(help);
	}
	public static void main(String[] args) {
		new GameGraph().setVisible(true);
	}
	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub
		if(!paint.reSizePaint(this.getHeight(), this.getWidth()))
			this.setSize(900, 720);
		
	}
	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

}
